package com.yunchang.springboot.scheduled.amazon.mws.report.datadispose;

import com.yunchang.springboot.models.Shop;
import com.yunchang.springboot.models.mwsreportbean.MwsCustomerShipmentSales;
import com.yunchang.springboot.models.mwsreportbean.MwsCustomerShipmentSalesDO;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.parent.ReportDataDisposeParent;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.reportinterface.IReportDataDispose;
import com.yunchang.springboot.utils.DateUtil;
import com.yunchang.springboot.utils.ToolUtil;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author czw
 * @date 2020/7/28 0028  18:44
 * @description:
 */
@Component
public class CompleteOrderDataDispose extends ReportDataDisposeParent implements IReportDataDispose {
    /**
     * 已完成订单 将获取到的数据转为实体对象数组
     *
     * @param lists
     * @param shopName
     * @param area
     * @return
     * @throws Exception
     */
    @Override
    public List<Object> reportDataDispose(List<List<String>> lists, String shopName, String area,String sellerId) throws Exception {
        List<String> titleList = lists.get(0);
        Map<String, Object> titleMap = new HashMap<>(titleList.size());
        for (int i = 0; i < titleList.size(); i++) {
            titleMap.put(titleList.get(i), i);
        }
        if (null != area && area.equals(ESPECIALLY_AREA) && !shopName.equals(SPECIAL_SHOP_NAME_TWO) && !shopName.equals(SPECIAL_SHOP_NAME)) {
            int shipmentDateIndex = Integer.parseInt(String.valueOf(titleMap.get("Customer Shipment Date")));
            int skuIndex = Integer.parseInt(String.valueOf(titleMap.get("Merchant SKU")));
            int fnskuIndex = Integer.parseInt(String.valueOf(titleMap.get("FNSKU")));
            int asinIndex = Integer.parseInt(String.valueOf(titleMap.get("ASIN")));
            int fulfillmentCenterIdIndex = Integer.parseInt(String.valueOf(titleMap.get("FC")));
            int quantityIndex = Integer.parseInt(String.valueOf(titleMap.get("Quantity")));
            int amazonOrderIdIndex = Integer.parseInt(String.valueOf(titleMap.get("Amazon Order Id")));
            int currencyIndex = Integer.parseInt(String.valueOf(titleMap.get("Currency")));
            int itemPricePerUnitIndex = Integer.parseInt(String.valueOf(titleMap.get("Product Amount")));
            int shippingPriceIndex = Integer.parseInt(String.valueOf(titleMap.get("Shipping Amount")));
            int giftWrapPriceIndex = Integer.parseInt(String.valueOf(titleMap.get("Gift Amount")));
            int shipCityIndex = Integer.parseInt(String.valueOf(titleMap.get("Shipment To City")));
            int shipStateIndex = Integer.parseInt(String.valueOf(titleMap.get("Shipment To State")));
            int shipPostalCodeIndex = Integer.parseInt(String.valueOf(titleMap.get("Shipment To Postal Code")));
            int PointsGrantedIndex = Integer.parseInt(String.valueOf(titleMap.get("Points Granted")));
//            //获取数据库里面最新的日期
//            String latestDay = mwsCustomerShipmentSalesMapper.getLatestDay(sellerId, area);
            List<Object> insertList = new ArrayList<>(lists.size());
            for (int i = 1; i < lists.size(); i++) {
                List<String> list = lists.get(i);
//                if (!StringUtil.isBlank(latestDay)) {
//                    //如果当前获取的数据的时间和数据库时间一致或者早于数据库时间，本次获取数据将跳过
//                    if (DateUtil.compateTime(latestDay, DateUtil.ISO8601ToGMT(list.get(shipmentDateIndex))) >= 0) {
//                        continue;
//                    }
//                }
                MwsCustomerShipmentSalesDO mwsCustomerShipmentSales = new MwsCustomerShipmentSalesDO();
                mwsCustomerShipmentSales.setSellerId(sellerId);
                mwsCustomerShipmentSales.setArea(area);
                //转为西零时区时间
                mwsCustomerShipmentSales.setShipmentDate(DateUtil.ISO8601ToGMT(list.get(shipmentDateIndex)));
                mwsCustomerShipmentSales.setSku(list.get(skuIndex));
                mwsCustomerShipmentSales.setFnsku(list.get(fnskuIndex));
                mwsCustomerShipmentSales.setAsin(list.get(asinIndex));
                mwsCustomerShipmentSales.setFulfillmentCenterId(list.get(fulfillmentCenterIdIndex));
                mwsCustomerShipmentSales.setQuantity(ToolUtil.valueOfInteger(list.get(quantityIndex)));
                mwsCustomerShipmentSales.setAmazonOrderId(list.get(amazonOrderIdIndex));
                mwsCustomerShipmentSales.setCurrency(list.get(currencyIndex));
                mwsCustomerShipmentSales.setItemPricePerUnit(new BigDecimal(String.valueOf(list.get(itemPricePerUnitIndex))));
                mwsCustomerShipmentSales.setShippingPrice(new BigDecimal(String.valueOf(list.get(shippingPriceIndex))));
                mwsCustomerShipmentSales.setGiftWrapPrice(new BigDecimal(String.valueOf(list.get(giftWrapPriceIndex))));
                mwsCustomerShipmentSales.setShipCity(list.get(shipCityIndex));
                mwsCustomerShipmentSales.setShipState(list.get(shipStateIndex));
                mwsCustomerShipmentSales.setShipPostalCode(list.get(shipPostalCodeIndex));
                mwsCustomerShipmentSales.setPointsGranted(new BigDecimal(String.valueOf(list.get(PointsGrantedIndex))));
                mwsCustomerShipmentSales.setCreateTime(DateUtil.getDate());
                mwsCustomerShipmentSales.setUpdateTime(DateUtil.getDate());
                mwsCustomerShipmentSales.setFinanceStatus(1);
                insertList.add(mwsCustomerShipmentSales);
            }
            return insertList;
        } else {
            int shipmentDateIndex = Integer.parseInt(String.valueOf(titleMap.get("shipment-date")));
            int skuIndex = Integer.parseInt(String.valueOf(titleMap.get("sku")));
            int fnskuIndex = Integer.parseInt(String.valueOf(titleMap.get("fnsku")));
            int asinIndex = Integer.parseInt(String.valueOf(titleMap.get("asin")));
            int fulfillmentCenterIdIndex = Integer.parseInt(String.valueOf(titleMap.get("fulfillment-center-id")));
            int quantityIndex = Integer.parseInt(String.valueOf(titleMap.get("quantity")));
            int amazonOrderIdIndex = Integer.parseInt(String.valueOf(titleMap.get("amazon-order-id")));
            int currencyIndex = Integer.parseInt(String.valueOf(titleMap.get("currency")));
            int itemPricePerUnitIndex = Integer.parseInt(String.valueOf(titleMap.get("item-price-per-unit")));
            int shippingPriceIndex = Integer.parseInt(String.valueOf(titleMap.get("shipping-price")));
            int giftWrapPriceIndex = Integer.parseInt(String.valueOf(titleMap.get("gift-wrap-price")));
            int shipCityIndex = Integer.parseInt(String.valueOf(titleMap.get("ship-city")));
            int shipStateIndex = Integer.parseInt(String.valueOf(titleMap.get("ship-state")));
            int shipPostalCodeIndex = Integer.parseInt(String.valueOf(titleMap.get("ship-postal-code")));
            int pointsGrantedIndex = 9999;
            if (null != titleMap.get("points-granted")) {
                pointsGrantedIndex = Integer.parseInt(String.valueOf(titleMap.get("points-granted")));
            }
//            //获取数据库里面最新的日期
//            String latestDay = mwsCustomerShipmentSalesMapper.getLatestDay(sellerId, area);
            List<Object> insertList = new ArrayList<>(lists.size());
            for (int i = 1; i < lists.size(); i++) {
                List<String> list = lists.get(i);
//                if (!StringUtil.isBlank(latestDay)) {
//                    //如果当前获取的数据的时间和数据库时间一致或者早于数据库时间，本次获取数据将跳过
//                    if (DateUtil.compateTime(latestDay, DateUtil.ISO8601ToGMT(list.get(shipmentDateIndex))) >= 0) {
//                        continue;
//                    }
//                }
                MwsCustomerShipmentSalesDO mwsCustomerShipmentSales = new MwsCustomerShipmentSalesDO();
                mwsCustomerShipmentSales.setSellerId(sellerId);
                mwsCustomerShipmentSales.setArea(area);
                //转为零时区时间
                mwsCustomerShipmentSales.setShipmentDate(DateUtil.ISO8601ToGMT(list.get(shipmentDateIndex)));
                mwsCustomerShipmentSales.setSku(list.get(skuIndex));
                mwsCustomerShipmentSales.setFnsku(list.get(fnskuIndex));
                mwsCustomerShipmentSales.setAsin(list.get(asinIndex));
                mwsCustomerShipmentSales.setFulfillmentCenterId(list.get(fulfillmentCenterIdIndex));
                mwsCustomerShipmentSales.setQuantity(ToolUtil.valueOfInteger(list.get(quantityIndex)));
                mwsCustomerShipmentSales.setAmazonOrderId(list.get(amazonOrderIdIndex));
                mwsCustomerShipmentSales.setCurrency(list.get(currencyIndex));
                mwsCustomerShipmentSales.setItemPricePerUnit(new BigDecimal(String.valueOf(list.get(itemPricePerUnitIndex))));
                mwsCustomerShipmentSales.setShippingPrice(new BigDecimal(String.valueOf(list.get(shippingPriceIndex))));
                mwsCustomerShipmentSales.setGiftWrapPrice(new BigDecimal(String.valueOf(list.get(giftWrapPriceIndex))));
                mwsCustomerShipmentSales.setShipCity(list.get(shipCityIndex));
                mwsCustomerShipmentSales.setShipState(list.get(shipStateIndex));
                mwsCustomerShipmentSales.setShipPostalCode(list.get(shipPostalCodeIndex));
                mwsCustomerShipmentSales.setCreateTime(DateUtil.getDate());
                mwsCustomerShipmentSales.setUpdateTime(DateUtil.getDate());
                if (pointsGrantedIndex != 9999) {
                    mwsCustomerShipmentSales.setPointsGranted(new BigDecimal(String.valueOf(list.get(pointsGrantedIndex))));
                }
                mwsCustomerShipmentSales.setFinanceStatus(1);
                insertList.add(mwsCustomerShipmentSales);
            }
            return insertList;
        }
    }
}
